package com.hirisun.lcdp.redis.key;

import com.up.redis.key.RedisEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

/**
 * @author: baiMl
 * @title: RedisKey
 * @projectName: 海联技术中台V2.0
 * @depart 软件事业部
 * @description:
 * @date: 2022/5/19 15:35
 */
public final class RedisKey {

    public final static String REDIS_KEY = "hlcdp:%s:%s";

    public final static String INSTANCE_CUU_MONTH = "instance:%s";

    public final static String DISTRIBUTED_KEY = "distributed";

    public final static String UNIFIED_PREFIX_KEY = "unified:%s:%s";

    public final static String WS_CACHE = "worksheet_id:%s";


    /**
     * 统一认证用户缓存KEY
     */
    public static String UNIFIED_USER_DETAIL_KEY = wrapperUnifiedRedisKey("init:detail:user:%s");
    /**
     * 统一认证组织缓存KEY
     */
    public static String UNIFIED_ORGAN_DETAIL_KEY = wrapperUnifiedRedisKey("init:detail:org:%s");

    /**
     * 统一认证组织缓存KEY
     */
    public static String UNIFIED_POSITION_DETAIL_KEY = wrapperUnifiedRedisKey("init:detail:position:%s");
    /**
     * 统一认证职级缓存KEY
     */
    public static String UNIFIED_POST_RANK_DETAIL_KEY = wrapperUnifiedRedisKey("init:detail:rank:%s");
    public static String LICENSE_KEY = "license:info";

    /**
     * 统一认证tokenKey
     */
    public static String TOKEN_KEY = wrapperRedisKey("token-key:%s");

    /**
     * 技术中台系统用户登录认证会话信息
     */
    public static String USER_SESSION_KEY = wrapperRedisKey("system:%s:session:user:%s");

    /**
     * 技术中台系统项目缓存
     */
    public static String HLCDP_PROJECT_KEY = wrapperRedisKey("system:%s:project:%s");

    /**
     * 用户菜单缓存前缀
     */
    public static String USER_APP_MENU_CACHE_PREFIX_KEY = wrapperRedisKey("system:%s:user:%s:menu:*");

    /**
     * 用户根菜单缓KEY
     */
    public static String USER_ROOT_MENU_CACHE_PREFIX_KEY = wrapperRedisKey("system:%s:user:%s:menu:rm");

    /**
     * 技术中台菜单树缓存前缀
     */
    public static String SYS_MENU_CACHE_PREFIX_KEY = wrapperRedisKey("allMenuSimpleTree=>sysId:%s*");

    /**
     * 用户表单缓存前缀
     */
    public static String USER_CACHE_PREFIX_KEY = wrapperRedisKey("system:%s:user:%s:*");

    /**
     * `
     * 用户表单缓存前缀
     */
    public static String USER_FORM_CACHE_PREFIX_KEY = wrapperRedisKey("system:%s:user:%s:form:*");

    /**
     * 技术中台系统用户根菜单下菜单缓存(树型结构)
     */
    public static String USER_APP_MENU_TREE_KEY = wrapperRedisKey("system:%s:user:%s:menu:amt:%s:rg:%s");

    /**
     * 技术中台系统用户功能菜单下功能权限缓存（菜单功能按钮缓存）
     */
    public static String USER_FUNC_MENU_KEY = wrapperRedisKey("system:%s:user:%s:menu:mf:%s");

    /**
     * 技术中台系统用户功能菜单下功能权限缓存（菜单功能按钮权限）
     */
    public static String USER_MENU_FUNC_AUTH_KEY = wrapperRedisKey("system:%s:user:%s:menu:mfa:%s");

    /**
     * 流程实例工作节点key排序key
     */
    public static String FLOW_INSTANCE_WORK_KEY = wrapperRedisKey("flow:instance:work:%s");
    public static String WORK_INSTANCE_SN_KEY = wrapperRedisKey("node:instance:workSn:%s");
    public static String FLOW_INSTANCE_TRACK_GROUP_KEY = wrapperRedisKey("node:instance:track:group:%s");

    public final static String MERGE_EDGE_LOCK_KEY = "lock:mergeEdge:%s";
    public final static String ARTIFICIAL_LOCK_KEY = "lock:artificial:%s:%s";
    /**
     * 配置审批的第一个节点的分布式锁
     */
    public final static String FIRST_APPROVAL_CONFIG_LOCK_KEY = "lock:approval:%d";
    /**
     * 子流程唤醒的分布式锁ID
     */
    public static final String LOCK_SUB_PROCESS_RELATION = "lock:subprocess:%d";

    public static final String HOLIDAY_KEY = "holiday";
    public static final String DESTROY_SERVER_HASH = "destroy:hash";

    public static final String LOCK_SCHEDULER = "lock:scheduler";
    public static final String LOCK_CLEAR_CONTEXT = "lock:context";

    public static final String LOCK_SERVER = "lock:server";

    public static final String LIC_LOCK = "lock:lic";

    public static final String LIC_REMAINDER = "lic:remainder";

    /**
     * 统一认证字典缓存KEY
     */
    public static String UNIFIED_DICT_DATA_KEY = wrapperUnifiedRedisKey("init:detail:dict:%s");
    /**
     * 技术中台系统项目列表缓存
     */
    public static String HLCDP_PROJECTS_KEY = wrapperRedisKey("system:%s:projects");
    /**
     * 技术中台系统项目缓存
     */
    public static String HLCDP_TENANT_KEY = wrapperRedisKey("system:%s:tenant:%s");

    /**
     * 技术中台菜单树缓存前缀
     */
    public static String SYS_SIMPLE_MENU_CACHE_PREFIX_KEY= wrapperRedisKey("allMenuSimpleTree=>sysId:%s");
    /**
     * 技术中台菜单树缓存
     */
    public static String SYS_MENU_CACHE_KEY = wrapperRedisKey("allMenuSimpleTree=>sysId:%s,menuPageType:%s,menuPageSubType:%s,appId:%s,keyword:%s");


    /**
     * OpenApi 权限缓存Key
     */
    public static String OpenAPI_CACHE_PREFIX_KEY = wrapperRedisKey("openapi:%s");
    /**
     * 节假日
     */
    public static final String FESTIVE = "festive:%s";

    public static String wrapperRedisKey(String currentKey) {
        Environment environment = RedisEnvironment.getEnvironment();
        String prefix = "dev";
        if (environment != null) {
            String[] activeProfiles = environment.getActiveProfiles();
            prefix = activeProfiles[0];
        }
        return String.format(REDIS_KEY, prefix, currentKey);
    }

    /**
     * 统一认证中心RedisKey
     *
     * @param currentKey
     * @return
     */


    public static String wrapperUnifiedRedisKey(String currentKey) {
        Environment environment = RedisEnvironment.getEnvironment();
        String env = environment.getProperty("hlcdp.unified.env");
        if (!StringUtils.hasText(env)) {
            throw new RuntimeException("容器未加载完成，请稍后才使用Redis key:" + currentKey);
        }
        return String.format(UNIFIED_PREFIX_KEY, env, currentKey);

    }
}
